package com.sl.hx.shop.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sl.hx.shop.constant.SPUKey;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索历史保存读取清空工具类
 * <p>
 * Created by hwg on 2017/12/12.
 */

public class SearchHistoryUtil {

    /**
     * 保存搜索记录
     *
     * @param key
     */
    public static void saveHistory(String key) {

        String oldSearch = SPUtils.getInstance().getString(SPUKey.SEARCHHISTORY);
        String newSearch = "";

        Map<String, String> oldSearchMap = null;

        if (!TextUtils.isEmpty(key)) {

            if (!TextUtils.isEmpty(oldSearch)) {

                oldSearchMap = new Gson().fromJson(oldSearch, new TypeToken<Map<String, String>>() {}.getType());

                if (oldSearchMap.containsValue(key)){
                    for (Map.Entry<String, String> entry : oldSearchMap.entrySet()) {
                        if (key.equals(entry.getValue())) {
                            oldSearchMap.remove(entry.getKey());
                            oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key);
                            break;
                        }
                    }
                }else{
                    oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key) ;
                }

                newSearch = new Gson().toJson(oldSearchMap);

            }else{
                oldSearchMap = new HashMap<>();
                oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key);
                newSearch = new Gson().toJson(oldSearchMap);
            }

            SPUtils.getInstance().put(SPUKey.SEARCHHISTORY, newSearch);
        }

    }

    /**
     * 获取搜索历史
     *
     * @return
     */
    public static List<String> getHistory(int getCount) {

        String searchHistory = SPUtils.getInstance().getString(SPUKey.SEARCHHISTORY);

        //最终排序后的历史记录列表
        List<String> result = new ArrayList<>();

        if (!TextUtils.isEmpty(searchHistory)) {
            Map<String, String> searchHistoryMap = new Gson().fromJson(searchHistory, new TypeToken<Map<String, String>>() {
            }.getType());

            Object[] keys = searchHistoryMap.keySet().toArray();


//            1.方法一，手动转换类型，用冒泡排序根据搜索的时间重新排序
//            //将Object数组先转换成String数组
//            String[] stringKeys = new String[keys.length] ;
//            for (int i = 0; i < keys.length; i++) {
//                stringKeys[i] = String.valueOf(keys[i]) ;
//            }
//
//            //再将String数组转换成Long数组
//            Long[] longKeys = new Long[keys.length] ;
//            for (int i = 0; i < stringKeys.length; i++) {
//                longKeys[i] = Long.valueOf(stringKeys[i]);
//            }
//
//            for (int i = 0; i < keys.length; i++) {
//                for (int j = 0; j < keys.length - i - 1; j++) {
//                    if (longKeys[j] < longKeys[j + 1]) {
//                        Long temp = longKeys[j];
//                        longKeys[j] = longKeys[j + 1];
//                        longKeys[j + 1] = temp;
//                    }
//                }
//            }
//
//            for (int i = 0; i < longKeys.length; i++) {
//                result.add(searchHistoryMap.get(String.valueOf(longKeys[i])));
//            }


//            方法二，自定义Comparator类，使用Arrays.sort()实现从大到小排序
            Arrays.sort(keys, new MyComparator());

            int showCount = keys.length > getCount ? getCount : keys.length ;
            for (int i = 0; i < showCount; i++) {
                result.add(searchHistoryMap.get(keys[i])) ;
            }

        }

        return result;

    }


    /**
     * 保存客户信息搜索记录
     *
     * @param key
     */
    public static void saveCustomerInfoHistory(String key) {

        String oldSearch = SPUtils.getInstance().getString(SPUKey.CUSTOMINFOSEARCHHISTORY);
        String newSearch = "";

        Map<String, String> oldSearchMap = null;

        if (!TextUtils.isEmpty(key)) {

            if (!TextUtils.isEmpty(oldSearch)) {

                oldSearchMap = new Gson().fromJson(oldSearch, new TypeToken<Map<String, String>>() {}.getType());

                if (oldSearchMap.containsValue(key)){
                    for (Map.Entry<String, String> entry : oldSearchMap.entrySet()) {
                        if (key.equals(entry.getValue())) {
                            oldSearchMap.remove(entry.getKey());
                            oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key);
                            break;
                        }
                    }
                }else{
                    oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key) ;
                }

                newSearch = new Gson().toJson(oldSearchMap);

            }else{
                oldSearchMap = new HashMap<>();
                oldSearchMap.put(String.valueOf(System.currentTimeMillis()), key);
                newSearch = new Gson().toJson(oldSearchMap);
            }

            SPUtils.getInstance().put(SPUKey.CUSTOMINFOSEARCHHISTORY, newSearch);
        }

    }

    /**
     * 获取搜索历史
     *
     * @return
     */
    public static List<String> getCustomerInfoHistory(int getCount) {

        String searchHistory = SPUtils.getInstance().getString(SPUKey.CUSTOMINFOSEARCHHISTORY);

        //最终排序后的历史记录列表
        List<String> result = new ArrayList<>();

        if (!TextUtils.isEmpty(searchHistory)) {
            Map<String, String> searchHistoryMap = new Gson().fromJson(searchHistory, new TypeToken<Map<String, String>>() {
            }.getType());

            Object[] keys = searchHistoryMap.keySet().toArray();

            Arrays.sort(keys, new MyComparator());

            int showCount = keys.length > getCount ? getCount : keys.length ;
            for (int i = 0; i < showCount; i++) {
                result.add(searchHistoryMap.get(keys[i])) ;
            }

        }

        return result ;
    }


    /**
     * 自定义的Comparator类，实现Arrays.sort()方法的从大到下的排序方式
     */
    private static class MyComparator implements Comparator<Object> {

        @Override
        public int compare(Object o1, Object o2) {
            String stringO1 = String.valueOf(o1) ;
            String stringO2 = String.valueOf(o2) ;

            Long longO1 = Long.valueOf(stringO1) ;
            Long longO2 = Long.valueOf(stringO2) ;

            if (longO1 < longO2){
                return 1;
            }else if (longO1 > longO2){
                return -1 ;
            }else{
                return 0 ;
            }

        }
    }


}
